package com.ml.doctor.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.google.gson.Gson;
import com.ml.doctor.R;
import com.ml.doctor.bean.CityBean;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/8 0008.
 */
public class PickSelectThreePop extends PopupWindow implements View.OnClickListener, PickerView.OnItemSelectedListener {
    PickerView idProvince;
    PickerView idCity;
    PickerView idDistrict;
    ImageView cancelImage;
    ImageView confirmImage;
    private Activity activity = null;
    CityBean cityModel;
    ConfirmListen confirmListen;
    int province = 0;
    int city = 0;
    int district = 0;


    public PickSelectThreePop(Activity activity) {
        this.activity = activity;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(activity).inflate(R.layout.pop_select_three, null);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(0));
        setTouchable(true);

        idProvince = (PickerView) view.findViewById(R.id.id_province);
        idProvince.setOnSelectListener(this);

        idCity = (PickerView) view.findViewById(R.id.id_city);
        idCity.setOnSelectListener(this);

        idDistrict = (PickerView) view.findViewById(R.id.id_district);
        idDistrict.setOnSelectListener(this);

        confirmImage = (ImageView) view.findViewById(R.id.confirm_image);
        confirmImage.setOnClickListener(this);

        cancelImage = (ImageView) view.findViewById(R.id.cancel_image);
        cancelImage.setOnClickListener(this);
        initList();
    }

    private void initList() {
        String str = getFromAssets("city.txt");
        cityModel = new Gson().fromJson(str, CityBean.class);
        idProvince.setList(cityModel.getProvince(), 0);
        idCity.setList(cityModel.getProvince().get(0).getCities(), 0);
        idDistrict.setList(cityModel.getProvince().get(0).getCities().get(0).getDistricts(), 0);
    }

    public String getFromAssets(String fileName) {
        String Result = "";
        try {
            InputStreamReader inputReader = new InputStreamReader(activity.getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result;
    }


    @Override
    public void onItemSelected(PickerView pickView, String text, int position) {
        if (pickView == idProvince) {
            province = position;
            idCity.setList(cityModel.getProvince().get(position).getCities(), 0);
            idDistrict.setList(cityModel.getProvince().get(position).getCities().get(0).getDistricts(), 0);
        } else if (pickView == idCity) {
            city = position;
            idDistrict.setList(cityModel.getProvince().get(province).getCities().get(position).getDistricts(), 0);
        } else {
            district = position;
        }

    }

    public void setConfirmListen(ConfirmListen confirmListen) {
        this.confirmListen = confirmListen;
    }

    @OnClick({R.id.cancel_image, R.id.confirm_image})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_image:
                dismiss();
                break;
            case R.id.confirm_image:
                if (confirmListen != null)
                    confirmListen.onConfirmListen(cityModel.getProvince().get(province).name + ""
                            + cityModel.getProvince().get(province).getCities().get(city).name + ""
                            + cityModel.getProvince().get(province).getCities().get(city).getDistricts().get(district).name);
                dismiss();
                break;
        }
    }

    public interface ConfirmListen {
        void onConfirmListen(String str);
    }


}
